package com.xiaopang.order.service.service.impl;

import com.xiaopang.order.service.dataobject.OrderDetail;
import com.xiaopang.order.service.dataobject.OrderMaster;
import com.xiaopang.order.service.dto.OrderDTO;
import com.xiaopang.order.service.enums.OrderStatusEnum;
import com.xiaopang.order.service.enums.PayStatusEnum;
import com.xiaopang.order.service.repository.OrderDetailRepository;
import com.xiaopang.order.service.repository.OrderMasterRepository;
import com.xiaopang.order.service.service.OrderService;
import com.xiaopang.order.service.utils.KeyUtil;
import com.xiaopang.product.client.ProductClient;
import com.xiaopang.product.common.DecreaseStockInput;
import com.xiaopang.product.common.ProductInfoOutput;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Autowired
    private ProductClient productClient;

    @Override
    public OrderDTO create(OrderDTO orderDTO) {

        String orderId = KeyUtil.genUniqueKey();

        //查询商品信息(调用商品服务)
        List<OrderDetail> orderDetailList = orderDTO.getOrderDetailList();
        List<String> productIdList = orderDetailList.stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());
        List<ProductInfoOutput> productInfoList = productClient.listForOrder(productIdList);
        //计算总价
        BigDecimal orderAmount = BigDecimal.ZERO;
        for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
            for (ProductInfoOutput productInfo : productInfoList) {
                if (orderDetail.getProductId().equals(productInfo.getProductId())) {
                    //单价*数量
                    orderAmount = orderAmount.add(
                            productInfo.getProductPrice()
                                    .multiply(new BigDecimal(orderDetail.getProductQuantity())));
                    //赋值参数
                    BeanUtils.copyProperties(productInfo, orderDetail);
                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());
                    //订单详情入库
                    orderDetailRepository.save(orderDetail);
                }
            }
        }
        //扣库存(调用商品服务)
        List<DecreaseStockInput> cartDTOList = orderDTO.getOrderDetailList().stream()
                .map(e -> new DecreaseStockInput(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productClient.decreateStock(cartDTOList);
        //订单入库
        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMasterRepository.save(orderMaster);
        return orderDTO;
    }
}
